import json

import requests
import execjs

from concurrent.futures import ThreadPoolExecutor


class Defense:
    def __init__(self):
        self.headers = {
            "Accept": "application/json, text/plain, */*",
            "Accept-Language": "zh-CN",
            "Cache-Control": "no-cache",
            "Connection": "keep-alive",
            "Pragma": "no-cache",
            "Sec-Fetch-Dest": "empty",
            "Sec-Fetch-Mode": "cors",
            "Sec-Fetch-Site": "same-origin",
            "En2uwnjgxodjhogi": "1",
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36",
            "xhnjc3odeyndrhmj": "CAYGOT0nJS4bGhM+Fg0EFQoCOQ==",
            "Rdmje1ymjkyzrknj": 'CQUGOh5IChICGhAVACoKBxxUTlY6EQUTExddb28tM2p/ZxwhJCIkODJsYH4zIzAjJT8lHjs9MT44f2kCYEJKSUIECAZIXENFQX1GVx8IEgEFB10IXFMCBwYPBW42NjZhZzkwbTc9PTI1aWVyIn9yNTU0d3V3b3ZvQXt7wdfAgtGGho2Ljt7didmIjs+SwsbDlcySycDIyZObn8vzpvvh6+uiobK9pqigveD1tby+o735976uv7ivnL23v8fiztrK0oidn5PXbA=='
        }

        self.cookies = {
            "cngizm2ziytq3mwy": "c9S+6wCmzFLxlgr/lw/5nF1XO6FQaKIBavhdaa8PZ/4HfrYbmg==",
            "fogq4n2exnzc0otg": "BAYDOQdeAlNeWlMMCFRcWkITRUITEBJHHUhBSRkYGRwlcCIkAQ=="
        }

        self.url = "https://adworld.xctf.org.cn/api/event/release_event/list/"

    def get_data(self, page):
        with open("攻防世界.js", encoding="utf-8") as f:
            js_code = f.read()
        query = {
            "event_status": "",
            "event_type": "",
            "isSearch": "false",
            "page": page,
            "page_size": "20",
            "search": ""
        }
        # 加密参数
        param = execjs.compile(js_code).call("get_param", query)
        params = {
            "qmze1yzvhyzcyyjr": param
        }
        print(params)

        response = requests.get(self.url, headers=self.headers, cookies=self.cookies, params=params)
        # 解密数据（请求头不带 xhnjc3odeyndrhmj 返回的是明文）
        res = execjs.compile(js_code).call("decrypt_data", response.text)
        result = json.loads(res)
        for data in result['data']['rows']:
            item = dict()
            item['release_name'] = data['release_name']
            item['release_sponsor'] = data['release_sponsor']
            item['competition_start_time'] = data['competition_start_time']
            item['competition_end_time'] = data['competition_end_time']
            print(item)


if __name__ == '__main__':
    d = Defense()
    with ThreadPoolExecutor(max_workers=5) as pool:
        for page in range(3):
            pool.submit(d.get_data, page)
